//
//  Accueil.m
//  Truffle
//
//  Created by Ismaila Kredity on 07/02/10.
//  Copyright 2010 kredity. All rights reserved.
//

#import "Accueil.h"
#import "GatewayConnection.h"
#import "Settings.h"
#import "DatasTruffle.h"
#import "LoadAppli.h"

@implementation Accueil

@synthesize greeting, nameInput, addNavigationController, setController, gateway, gatewayConnection;


/*
 * Vue à été loadée
 */
- (void)viewDidLoad {
	[super viewDidLoad];
	
	[self loadApplication];
	
	[disconnectedImageView removeFromSuperview];
	/*
	UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] 
									 initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh   
									 target:self 
									 action:@selector(refreshAction)];
	[self.navigationItem setRightBarButtonItem:reloadButton];
	*/
	self.title = @"Truffle";
	
	
	
	// En cas d'erreur on n'est pas connectés.
	// if (result < 0) connected = FALSE;
	
	// Si pas de connexion, on affiche un message d'erreur.
	/*
	if (!connected) {
		[activityIndicator startAnimating];
		[self.view addSubview:disconnectedImageView];
		disconnectedImageView.alpha=0.0;
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:2.0];
		disconnectedImageView.alpha=1.0;
		[UIView commitAnimations];
		[activityIndicator stopAnimating];
		[activityIndicator removeFromSuperview];
		[activityIndicator release];
	}
	 */
	
	//[disconnectedImageView removeFromSuperview];
	
	// Ajout des boutons.
}

- (void) helpClicked:(id)sender {
	
	NSString * message = @"Aide";
	NSString * messageText = @"Bienvenue sur la plate-forme Truffle. Vous avez accès aux sections suivantes :\n - Truffle : page de démarrage. Vous pouvez y modifier vos paramètres de connexion via "
							   "le bouton de préférences.\n - Services : vous permet de consulter vos services accessibles.\n - Contacts : vous permet de récupérer la liste des contacts dans votre zone "
							   "ainsi que vos contacts favoris.\n - Coupons : vous permet de visualiser et échanger vos coupons.\nTruffle est une création de Double Quinquérème Software. Pour plus d'informations, "
							   "merci de consulter notre site web.";
	
//	NSString * URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
//	NSString * msg = (URLString != NULL ) ? @"YES" : @"NO";
	
	
	UIAlertView *alert = [[UIAlertView alloc]
						  initWithTitle:message
						  message:[NSString stringWithFormat:@"%@", messageText]
						  delegate:nil
						  cancelButtonTitle:nil
						  otherButtonTitles:@"OK",  nil];
	[alert show];
	[alert release];
}



-(IBAction) settings:(id)sender
{
	if(setController == nil)
		setController = [[Settings alloc] initWithNibName:@"Settings" bundle:nil];
	if(setNavigationController == nil)
		setNavigationController = [[UINavigationController alloc] initWithRootViewController:setController];
	
	// On enregistre un pointeur vers l'accueil ici.
	[setController setHome:self];
	
	[self.navigationController presentModalViewController:setNavigationController animated:YES];
}

- (void) loadApplication {
	if(loadAppli == nil)
		loadAppli = [[LoadAppli alloc] initWithNibName:@"LoadAppli" bundle:nil];
	
	[loadAppli setHome:self];
	
	if(addNavigationController == nil)
		addNavigationController = [[UINavigationController alloc] initWithRootViewController:loadAppli];
	
	[self.navigationController presentModalViewController:addNavigationController animated:NO];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)donePerformingRequests {
	
	// FIXME : connected devrait être une variable globale, apparemment non.
	//if (!connected) {
		[activityIndicator startAnimating];
		disconnectedImageView.alpha=0.0;
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:2.0];
		disconnectedImageView.alpha=1.0;
		[UIView commitAnimations];
		[activityIndicator stopAnimating];
		[activityIndicator removeFromSuperview];
		[activityIndicator release];
	//}
	
	// On ajoute les deux boutons info et settings.
	UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
	infoButton.frame = CGRectMake(295, 7, 18, 18);
    [infoButton addTarget:self action:@selector(helpClicked:) forControlEvents:UIControlEventTouchUpInside];
	
	//NSLog(@"OMAGAAAAAAD! %@", [DatasTruffle isConnected]);
	
	if ([DatasTruffle isConnected])
	{
		// FIXME : renommer la variable.
		[disconnectedImageView setImage:[UIImage imageNamed:@"connected_message.png"]];
	}
		
	[self.view addSubview:disconnectedImageView];
	
	UIImage *settingImg = [UIImage imageNamed:@"settings.png"];
	UIButton *settingButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
	settingButton.frame = CGRectMake(270, 7, settingImg.size.width, settingImg.size.height);
	[settingButton setImage:settingImg forState:UIControlStateNormal];
	[settingButton addTarget:self action:@selector(settings:) forControlEvents:UIControlEventTouchUpInside];	
	
	[self.view addSubview:infoButton];
	[self.view addSubview:settingButton];
}

- (void)performingRequestsError {
	[self donePerformingRequests];
}

- (void)dealloc {
	//[tableData release];
	//[restConnection release];
    [super dealloc];
}


@end